package com.hjs.test.jst;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class TokenUtils {
    private static final String initToken = "0fe9971e5cd94866bb878fc9464436d4";
    private static final String refreshToken = "ee0a03e2b2b54504b2e371de64bd80f5";
    private static final String TOKEN_KEY = "jushuitan:ysc:token";
    private static String tokenInRedis;
    private static final long TOKEN_KEY_TTL = 20;
    public static String getToken(){
        tokenInRedis = (String) RedissonClientHolder.getRedissonClient().getBucket(TOKEN_KEY).get();
        if(Objects.isNull(tokenInRedis)){
            synchronized (TokenUtils.class){
                if(Objects.isNull(tokenInRedis)){
                    tokenInRedis = achieveToken();
                    RedissonClientHolder.getRedissonClient().getBucket(TOKEN_KEY).set(tokenInRedis, TOKEN_KEY_TTL, TimeUnit.SECONDS); // 存入Redis
                }
            }
        }
        return tokenInRedis;
    }

    private static String achieveToken(){
        return "";
    }


    static class RedissonClientHolder{
        static volatile RedissonClient redissonClient;
        private static RedissonClient getRedissonClient(){
            if(Objects.isNull(redissonClient)){
                synchronized (Object.class){
                    if(Objects.isNull(redissonClient)) {
                        Config config = new Config();
                        config.useSingleServer().setAddress("redis://200.100.105.34:6379");
                        redissonClient = Redisson.create(config);
                    }
                }

            }
            return redissonClient;
        }
    }
}
